﻿<cfscript>
/** 
* @hint "cetManager, cetApply - CET"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.cet.cetListener" 
	output=false 
	depends="sessionAdvice, datetimeAdvice, cetEntryPropertyAdvice, cetPropertyAdvice, sequenceCETEntry"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 

	/**
	* @hint "创建新报名记录"
	* 
	* @event "MachII Event Object"
	*/
	public void function createCetEntry( required MachII.framework.Event event ) output=false {
				
		var studentId = getsessionAdvice().getAutherUserID();
		var studentGrade = getsessionAdvice().getUserProp("studentGrade");
		var studentDepartment = getsessionAdvice().getUserProp("studentDepartment");
		
		var termId = event.getArg("TermID");
		var cetId = "";

		try {
		
			var sign = event.getArg("ST");
			var key = studentId & studentGrade & studentDepartment;
		
			cetId = decrypt(sign, key, "PBEWithMD5AndDES", "Hex");
		}
		catch ( any expt ) {
		}
		
		if ( cetId neq "" ) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {

				sql = "SELECT a.eid 
						FROM t_cet_entry a
						WHERE a.term_id = :termId 
							  AND
							  a.stu_id = :studentId ";
				queryObj = new Query( datasource = application.dnsMaster );
				queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
				var rs_entry = queryObj.execute( sql=sql ).getResult();
				
				if ( rs_entry.recordCount eq 0 ) {
					
					var sequenceCETEntry = getsequenceCETEntry();
					sequenceCETEntry.setTermID(termId);
					sequenceCETEntry.setStudentID(studentId);						
					var entryId = sequenceCETEntry.getID();

					var propAdvice = getcetEntryPropertyAdvice().init();
				
					switch ( event.getArg("P") ) {
						case "Cash":
							propAdvice.setCash(true);
							break;
						case "BankInstead":
							propAdvice.setBankInstead(true);
							break;
						case "BankOnline":
							propAdvice.setBankOnline(true);
							break;
					}
					
					sql = "INSERT INTO t_cet_entry (eid,
													term_id,
													stu_id,
													exam_id,
													is_charged,
													apply_date,
													entry_prop 
													) 
											VALUES
											 ( :entryId ,
											   :termId ,
											   :studentId ,
											   :cetId ,
											   :isCharged ,
											   :applyDate ,
											   :entryProp  )";
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="entryId", value=entryId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="cetId", value=cetId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="isCharged", value="0", cfsqltype="cf_sql_char" );
					queryObj.addParam( name="applyDate", value=getdatetimeAdvice().getCurrentDatetimeString(), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="entryProp", value= propAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );
					
				}
				else {
					var propAdvice = getcetEntryPropertyAdvice().init();
				
					switch ( event.getArg("P") ) {
						case "Cash":
							propAdvice.setCash(true);
							break;
						case "BankInstead":
							propAdvice.setBankInstead(true);
							break;
						case "BankOnline":
							propAdvice.setBankOnline(true);
							break;
					}
					
					sql = " UPDATE t_cet_entry a 
							   SET a.exam_id = :cetId ,
								   a.is_charged = :isCharged ,
								   a.apply_date = :applyDate ,
								   a.entry_prop = :entryProp  
							  WHERE 
							  		eid = :entryId ";
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="entryId", value=rs_entry.eid, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="cetId", value=cetId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="isCharged", value="0", cfsqltype="cf_sql_char" );
					queryObj.addParam( name="applyDate", value=getdatetimeAdvice().getCurrentDatetimeString(), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="entryProp", value= propAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );
				}

			}
			
		}
		
	}
	
	/**
	* @hint "保存报考科目设置"
	* 
	* @event "MachII Event Object"
	*/
	public void function saveCetSetting( required MachII.framework.Event event ) output=false {
		
		var cetId = event.getArg("ExamID");
		
		var sql = "";
		var queryObj = "";
		
		var propAdvice = getcetPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			/* 报名开始日期 */
			var dateStart = replace(event.getArg("DateStart"), "-", "", "ALL");
			/* 报名结束日期 */
			var dateEnd = replace(event.getArg("DateEnd"), "-", "", "ALL");
			
			/* 当天开始时间 */
			var timeStart = replace(event.getArg("TimeStart"), ":", "", "ALL");
			/* 当天结束时间 */
			var timeEnd = replace(event.getArg("TimeEnd"), ":", "", "ALL");

			/* 考试日期 */
			var examDate = replace(event.getArg("ExamDate"), "-", "", "ALL");
			/* 考试时间 */
			var examTime = replace(event.getArg("ExamTime"), ":", "", "ALL");
			
			
			/* 加载要更新对象 */
			sql = "SELECT exam_prop FROM t_cet WHERE exam_id = :cetId";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="cetId", value=cetId, cfsqltype="cf_sql_char" );

			var rs_entity = queryObj.execute( sql=sql ).getResult();
			
			var examProp = rs_entity.exam_prop;

			/* 是否启用报名 */
			propAdvice.parseProperty( examProp );
			
			if ( yesNoFormat( event.getArg("Enabled", "0") ) ) {
				propAdvice.setRunning( true );
			}
			else {
				propAdvice.setRunning( false );
			}
			
			examProp = propAdvice.stringProperty();
			
			/* 报名费 */
			var examCharge = event.getArg("ExamCharge");

			/* 更正允许报名时段 */
			if ( event.getArg("FullDay") eq "1" ) {
				timeStart = "0001";
				timeEnd = "2359";
			}

			/* 允许报名年级 */
			var gradesAllowed = "ALL";
			
			if ( event.getArg("FullGrade") neq "1" ) {
				gradesAllowed = listSort(event.getArg('targetGrade'), "text", "asc");
			}

			/* 允许报名学院 */
			var instituteAllowed = "ALL";
			if ( event.getArg("FullInstitute") neq "1" ) {
				instituteAllowed = listSort(event.getArg('targetInstitute'), "text", "asc");
			}
			
			/* 更新考试科目设置 */
			sql = "UPDATE t_cet 
					SET 
						date_start = :dateStart , 
						date_end = :dateEnd , 
						time_start = :timeStart , 
						time_end = :timeEnd , 
						exam_date = :examDate , 
						exam_time = :examTime , 
						exam_prop = :examProp , 
						exam_charge = :examCharge , 
						grade_allowed = :gradesAllowed , 
						institute_allowed = :instituteAllowed 
					WHERE 
						exam_id = :cetId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="cetId", value=cetId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="dateStart", value=dateStart, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="dateEnd", value=dateEnd, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="timeStart", value=timeStart, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="timeEnd", value=timeEnd, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examDate", value=examDate, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examTime", value=examTime, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="examProp", value=examProp, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="examCharge", value=examCharge, cfsqltype="cf_sql_decimal", scale=2 );
			queryObj.addParam( name="gradesAllowed", value=gradesAllowed, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="instituteAllowed", value=instituteAllowed, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
		}
		
	}
	
}

</cfscript>